﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NRemedy;
using System.Configuration;
using NRemedy.MVC.UI;


namespace RemedyMVC.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        protected static string ARServer = ConfigurationManager.AppSettings["ARServer"];

        public ActionResult Login()
        {
            ViewBag.loginResult = "";
            return View();
        }

        [HttpPost]
        public ActionResult Login(string name, string password, string ReturnUrl)
        {
            ARLoginContext context = null;
            UserSession us = null;
            try
            {
                context = new ARLoginContext(ARServer, name, password);
                var grouplist = context.ServerInstance.GetListGroup(name);
                us = new UserSession();
                us.ARContext = context;
                foreach (var g in grouplist)
                {
                    us.GroupList.Add(g.GroupName); 
                }
                //if (context.User == "Demo")
                //{
                //    us.GroupList.Add("问题管理经理");
                //    us.GroupList.Add("变更经理");
                //}
                Session[UserSession.SessionKey] = us;
                if (string.IsNullOrEmpty(ReturnUrl))
                    return RedirectToAction("Index", "Incident");
                return Redirect(ReturnUrl);
            }
            catch
            {
                if (us != null)
                    us.Dispose();
                ViewBag.loginResult = "alert('登录失败')";
                return View();
            }



        }


        public ActionResult Logout()
        {
            UserSession session = Session[UserSession.SessionKey] as UserSession;
            if (session != null)
            {
                session.Dispose();
            }
            Session[UserSession.SessionKey] = null;
            return RedirectToAction("Login", "Account");
 
        }

        private List<Customer> people = new List<Customer>();
        
                //       $('#536870920').val(record.phone);
                //$('#536870950').val(record.email);
                //$('#536870934').val(record.branch);
                //$('#536870929').val(record.location);
                //$('#536870928').val(record.dept);
                //$('#536870927').val(record.seat);

        public AccountController()
        {
            people.Add(new Customer{ name="Bob",phone = "123",email = "bobpan@wicresoft.com",branch="上海",location="福泉路",dept="EKA",seat="3F061"});
            people.Add(new Customer { name = "Parker", phone = "1234", email = "parkerz@wicresoft.com", branch = "上海", location = "福泉路", dept = "EKA", seat = "3F062" });
            people.Add(new Customer { name = "Jane", phone = "12345", email = "janeli@wicresoft.com", branch = "上海", location = "福泉路", dept = "EKA", seat = "3F063" });
            people.Add(new Customer { name = "Jibin", phone = "123456", email = "jibinw@wicresoft.com", branch = "上海", location = "宁桥路", dept = "EKA", seat = "3F064" });
            people.Add(new Customer { name = "Jack", phone = "1237", email = "jackw@wicresoft.com", branch = "上海", location = "福泉路", dept = "EKA", seat = "3F065" });
            people.Add(new Customer { name = "Smile", phone = "1238", email = "smilew@wicresoft.com", branch = "上海", location = "宁桥路", dept = "EKA", seat = "3F066" });
            people.Add(new Customer { name = "Jiayu", phone = "1239", email = "jiayuch@wicresoft.com", branch = "上海", location = "福泉路", dept = "EKA", seat = "3F067" });
            people.Add(new Customer { name = "Yongfeng", phone = "1230", email = "yonfengl@wicresoft.com", branch = "上海", location = "宁桥路", dept = "EKA", seat = "3F068" });
            people.Add(new Customer { name = "Bochen", phone = "12310", email = "bochen@wicresoft.com", branch = "上海", location = "宁桥路", dept = "EKA", seat = "3F069" });
        }


        public ActionResult People(string q)
        {
            //JsonBuilder build = new JsonBuilder(JsonBuilderRoot.Collection);
            //if (!string.IsNullOrEmpty(q))
            //{
            //    foreach (var p in people.Where(x => x.name.StartsWith(q)))
            //    {
            //        JsonBuilder b = new JsonBuilder();
            //        b.MergeProperty("text", p);
            //        b.MergeProperty("id", p);
            //        b.MergeProperty("prop1", "prop1");
            //        b.MergeProperty("prop2", "prop2");
            //        build.MergeCollectionObject(b);
            //    }
            //}
            //return new RawJsonResult { Data = build.ToString(), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            if (!string.IsNullOrEmpty(q))
                return Json(people.Where(x => x.name.StartsWith(q,StringComparison.OrdinalIgnoreCase)), JsonRequestBehavior.AllowGet);
            else
                return Json(people.Where(x=>false), JsonRequestBehavior.AllowGet);
 
        }


        private class Customer
        {
            public string name { get; set; }
            public string phone { get; set; }
            public string email { get; set; }
            public string branch { get; set; }
            public string location { get; set; }
            public string dept { get; set; }
            public string seat { get; set; }
        }
    }
}
